package com.yzd.netmonitor.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件类型检查
 * @author Administrator
 *
 */
public class FileTypeUtil {
	private final static Map<String, String> FILE_TYPE_ALL =new HashMap<String,String>();
	private final static Map<String, String> FILE_TYPE_PIC =new HashMap<String,String>();
	private final static Map<String, String> FILE_TYPE_MOVIE =new HashMap<String,String>();
	static{
		intFileTypeMapAll();
		intFileTypePic();
		intFileTypeMovie();
	}
	
	public enum TypeEnum {
		PIC("PIC",FILE_TYPE_PIC),
		ALL("ALL",FILE_TYPE_ALL),
		MOVIE("MOVIE",FILE_TYPE_MOVIE);
		private Map<String, String> value;
		private  TypeEnum(String index,Map<String, String> value){
			this.value = value;
		}
		private Map<String, String> getValue(){
			return value;
		}
	}
	
	/**
	 * 初始化所有文件类型map
	 */
	private static  void intFileTypeMapAll(){  
		  FILE_TYPE_ALL.put("jpg", "FFD8FF"); //JPEG (jpg)  
		  FILE_TYPE_ALL.put("png", "89504E47"); //PNG (png)  
		  FILE_TYPE_ALL.put("gif", "47494638"); //GIF (gif)  
		  FILE_TYPE_ALL.put("tif", "49492A00"); //TIFF (tif)  
		  FILE_TYPE_ALL.put("bmp", "424D"); //Windows Bitmap (bmp)  
	      FILE_TYPE_ALL.put("dwg", "41433130"); //CAD (dwg)  
	      FILE_TYPE_ALL.put("html", "68746D6C3E"); //HTML (html)  
	      FILE_TYPE_ALL.put("rtf", "7B5C727466"); //Rich Text Format (rtf)  
	      FILE_TYPE_ALL.put("xml", "3C3F786D6C");  
	      FILE_TYPE_ALL.put("zip", "504B0304");  
	      FILE_TYPE_ALL.put("rar", "52617221");  
	      FILE_TYPE_ALL.put("psd", "38425053"); //Photoshop (psd)  
	      FILE_TYPE_ALL.put("eml", "44656C69766572792D646174653A"); //Email [thorough only] (eml)  
	      FILE_TYPE_ALL.put("dbx", "CFAD12FEC5FD746F"); //Outlook Express (dbx)  
	      FILE_TYPE_ALL.put("pst", "2142444E"); //Outlook (pst)  
	      FILE_TYPE_ALL.put("xls", "D0CF11E0"); //MS Word  
	      FILE_TYPE_ALL.put("doc", "D0CF11E0"); //MS Excel 注意：word 和 excel的文件头一样  
	      FILE_TYPE_ALL.put("mdb", "5374616E64617264204A"); //MS Access (mdb)  
	      FILE_TYPE_ALL.put("wpd", "FF575043"); //WordPerfect (wpd)  
	      FILE_TYPE_ALL.put("eps", "252150532D41646F6265");  
	      FILE_TYPE_ALL.put("ps", "252150532D41646F6265");
	      FILE_TYPE_ALL.put("pdf", "255044462D312E"); //Adobe Acrobat (pdf)  
	      FILE_TYPE_ALL.put("qdf", "AC9EBD8F"); //Quicken (qdf)  
	      FILE_TYPE_ALL.put("pwl", "E3828596"); //Windows Password (pwl)  
	      FILE_TYPE_ALL.put("wav", "57415645"); //Wave (wav)  
	      FILE_TYPE_ALL.put("avi", "41564920");  
	      FILE_TYPE_ALL.put("ram", "2E7261FD"); //Real Audio (ram)  
	      FILE_TYPE_ALL.put("rm", "2E524D46"); //Real Media (rm)  
	      FILE_TYPE_ALL.put("mpg", "000001BA"); //  
	      FILE_TYPE_ALL.put("mov", "6D6F6F76"); //Quicktime (mov)  
	      FILE_TYPE_ALL.put("asf", "3026B2758E66CF11"); //Windows Media (asf)  
	      FILE_TYPE_ALL.put("mid", "4D546864"); //MIDI (mid)  
	}
	/**
	 * 初始化所有pic文件类型map
	 */
	private static  void intFileTypePic(){  
		  FILE_TYPE_PIC.put("jpg", "FFD8FF"); //JPEG (jpg)  
		  FILE_TYPE_PIC.put("png", "89504E47"); //PNG (png)  
		  FILE_TYPE_PIC.put("gif", "47494638"); //GIF (gif)  
		  FILE_TYPE_PIC.put("jpeg", "FFD8FF"); 
		  //FILE_TYPE_PIC.put("tif", "49492A00"); //TIFF (tif)  
		  FILE_TYPE_PIC.put("bmp", "424D"); //Windows Bitmap (bmp)  
	}
	
	/**
	 * 初始化所有movie文件类型map
	 */
	private static  void intFileTypeMovie(){  
		  FILE_TYPE_MOVIE.put("wav", "57415645"); //Wave (wav)  
		  FILE_TYPE_MOVIE.put("avi", "41564920");  
	      FILE_TYPE_MOVIE.put("ram", "2E7261FD"); //Real Audio (ram)  
	      FILE_TYPE_MOVIE.put("rm", "2E524D46"); //Real Media (rm)  
	      FILE_TYPE_MOVIE.put("mpg", "000001BA"); //  
	      FILE_TYPE_MOVIE.put("mov", "6D6F6F76"); //Quicktime (mov)  
	      FILE_TYPE_MOVIE.put("asf", "3026B2758E66CF11"); //Windows Media (asf)  
	      FILE_TYPE_MOVIE.put("mid", "4D546864"); //MIDI (mid) 
	      FILE_TYPE_MOVIE.put("m4a", "4D546864"); //MIDI (mid) 
	}
	
	/**
	 * 检查类型是否被篡改
	 * @param b
	 * @param type
	 * @return
	 */
	public static boolean checkFileTypeModifiedByStream(final byte[] b,String currentType,TypeEnum type) {  
	        return checkFileTypePermit(currentType, type) &&
	        		getFileHexString(b).toUpperCase().startsWith(type.getValue().get(currentType));  
	 }
	 
	 /**
	  * 检查是否允许的类型
	  * @param fileType
	  * @return
	  */
	 public static boolean checkFileTypePermit(String fileType,TypeEnum type){
		 if (type == null)
	            throw new NullPointerException("TypeEnum canot be null");
		 if(fileType==null){
			 return false;
		 }
		 return type.getValue().containsKey(fileType.toLowerCase());
	 }
	 
	 private static String getFileHexString(final byte[] b) {       
	        StringBuilder stringBuilder = new StringBuilder();       
	        if(b == null || b.length <= 0) {  
	            return null;  
	        }  
	        for (int i = 0; i < b.length; i++) {  
	            int v = b[i] & 0xFF;  
	            String hv = Integer.toHexString(v);  
	            if (hv.length() < 2) {  
	                stringBuilder.append(0);  
	            }  
	            stringBuilder.append(hv);  
	        }  
	        return stringBuilder.toString();  
	  }
	 
	 /*public static void main(String[] args) {
		System.out.println(checkFileTypeModifiedByStream(null, "jpg", TypeEnum.PIC));
	}*/
}
